package com.gisocn.onemap.ui.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.FragmentActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.gisocn.onemap.base.BaseActivity;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.model.ImageWall;
import com.gisocn.onemap.presenter.FileBrowsePresent;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.view.FileBrowseView;
import com.gisocn.onemap.widget.NumberProgressBar;
import com.google.gson.Gson;
import com.tencent.smtt.sdk.TbsReaderView;
import com.tencent.smtt.sdk.TbsVideoCacheTask;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import kotlin.text.Typography;

/* loaded from: classes2.dex */
public class FileBrowseActivity extends BaseActivity<FileBrowsePresent> implements TbsReaderView.ReaderCallback, FileBrowseView {
    private float curPosX;
    private float curPosY;
    private Dialog dialog;
    private FileBrowsePresent fileBrowsePresent;
    private String fileUrl;
    @BindView(R.id.iv_content)
    ImageView ivContent;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private String mFileName;
    private NumberProgressBar numberProgressBar;
    private float posX;
    private float posY;
    TbsReaderView tbsReaderView;
    private TextView tvFileName;
    private TextView tvIndex;
    @BindView(R.id.tv_no_file)
    TextView tvNoFile;
    private TextView tvOff;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private String url;
    private URL downLoadUrl = null;
    String filePath = "";
    ArrayList<ImageWall> imageWalls = new ArrayList<>();
    private int index = 0;
    Handler handler = new Handler() { // from class: com.gisocn.onemap.ui.activity.FileBrowseActivity.4
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i != 1) {
                if (i == 2 && FileBrowseActivity.this.numberProgressBar != null) {
                    FileBrowseActivity.this.numberProgressBar.setProgress(message.arg1);
                    if (message.arg1 == 100) {
                        FileBrowseActivity.this.dialog.dismiss();
                        return;
                    }
                    return;
                }
                return;
            }
            FileBrowseActivity.this.dismissProgressDialog();
            FileBrowseActivity.this.displayFile();
        }
    };

    @Override // com.gisocn.onemap.view.FileBrowseView
    public void downLoadFail() {
    }

    @Override // com.tencent.smtt.sdk.TbsReaderView.ReaderCallback
    public void onCallBackAction(Integer num, Object obj, Object obj2) {
    }

    static /* synthetic */ int access$408(FileBrowseActivity fileBrowseActivity) {
        int i = fileBrowseActivity.index;
        fileBrowseActivity.index = i + 1;
        return i;
    }

    static /* synthetic */ int access$410(FileBrowseActivity fileBrowseActivity) {
        int i = fileBrowseActivity.index;
        fileBrowseActivity.index = i - 1;
        return i;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_file_browse);
        ButterKnife.bind(this);
        init();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseActivity
    public FileBrowsePresent createPresenter() {
        return new FileBrowsePresent(this);
    }

    void init() {
        this.fileBrowsePresent = createPresenter();
        TbsReaderView tbsReaderView = new TbsReaderView(this, this);
        this.tbsReaderView = tbsReaderView;
        this.llContent.addView(tbsReaderView, new LinearLayout.LayoutParams(-1, -1));
        if (getIntent().getStringExtra(TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL) != null && getIntent().getStringExtra("fileName") != null) {
            this.url = getIntent().getStringExtra(TbsVideoCacheTask.KEY_VIDEO_CACHE_PARAM_URL);
            this.fileUrl = getIntent().getStringExtra("fileUrl");
            this.mFileName = getIntent().getStringExtra("fileName");
            if (getIntent().getSerializableExtra("imageWalls") != null) {
                this.imageWalls = (ArrayList) getIntent().getSerializableExtra("imageWalls");
            }
            if (this.imageWalls.size() > 0) {
                this.index = this.imageWalls.get(0).getIndex();
            }
            Log.e("imageWalls", new Gson().toJson(this.imageWalls));
            if (this.url.equals("local")) {
                String parent = new File(this.fileUrl).getParent();
                this.filePath = parent;
                Log.e(TbsReaderView.KEY_FILE_PATH, parent);
                displayFile();
            } else {
                try {
                    this.fileUrl = URLDecoder.decode(this.fileUrl, "utf-8");
                    this.mFileName = URLDecoder.decode(this.mFileName, "utf-8");
                    Log.e("str", this.fileUrl);
                    Log.e("mFileName", this.mFileName);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                this.tvNoFile.setVisibility(8);
                this.filePath = "";
                String[] split = this.fileUrl.replace(MainActivity.MEDIADATA_PATH_REPLACE, "").split("/");
                for (int i = 0; i < split.length - 1; i++) {
                    String str = this.filePath + split[i] + "/";
                    this.filePath = str;
                    Log.e(TbsReaderView.KEY_FILE_PATH, str);
                    File file = new File(Constant.SAVE_FILE_PACKAGE + this.filePath);
                    Log.e("!file.exists()", file.getAbsolutePath());
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                }
                if (isLocalExist()) {
                    displayFile();
                } else {
                    showDialog(this.mFileName);
                    this.fileBrowsePresent.downLoadFile(this.url, this.filePath, this.mFileName, new DownLoadListener() { // from class: com.gisocn.onemap.ui.activity.FileBrowseActivity.1
                        @Override // com.gisocn.onemap.interfaces.DownLoadListener
                        public void onFailure() {
                        }

                        @Override // com.gisocn.onemap.interfaces.DownLoadListener
                        public void onFinish(String str2) {
                        }

                        @Override // com.gisocn.onemap.interfaces.DownLoadListener
                        public void onStart() {
                        }

                        @Override // com.gisocn.onemap.interfaces.DownLoadListener
                        public void onProgress(int i2) {
                            Message message = new Message();
                            message.what = 2;
                            message.arg1 = i2;
                            FileBrowseActivity.this.handler.sendMessage(message);
                        }
                    });
                }
            }
        }
        TextView textView = this.tvTitle;
        String str2 = this.mFileName;
        textView.setText(str2.substring(0, str2.lastIndexOf(".")));
        this.ivContent.setOnTouchListener(new View.OnTouchListener() { // from class: com.gisocn.onemap.ui.activity.FileBrowseActivity.2
            @Override // android.view.View.OnTouchListener
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionEvent.getAction();
                if (action == 0) {
                    FileBrowseActivity.this.posX = motionEvent.getX();
                    FileBrowseActivity.this.posY = motionEvent.getY();
                } else if (action == 1) {
                    if (FileBrowseActivity.this.curPosX - FileBrowseActivity.this.posX <= 0.0f || Math.abs(FileBrowseActivity.this.curPosX - FileBrowseActivity.this.posX) <= 25.0f) {
                        if (FileBrowseActivity.this.curPosX - FileBrowseActivity.this.posX < 0.0f && Math.abs(FileBrowseActivity.this.curPosX - FileBrowseActivity.this.posX) > 25.0f && FileBrowseActivity.this.index < FileBrowseActivity.this.imageWalls.size() - 1 && FileBrowseActivity.this.url.equals("local")) {
                            FileBrowseActivity.access$408(FileBrowseActivity.this);
                            FileBrowseActivity.this.tvTitle.setText(FileBrowseActivity.this.imageWalls.get(FileBrowseActivity.this.index).getName().substring(0, FileBrowseActivity.this.imageWalls.get(FileBrowseActivity.this.index).getName().lastIndexOf(".")));
                            Glide.with((FragmentActivity) FileBrowseActivity.this).load(FileBrowseActivity.this.imageWalls.get(FileBrowseActivity.this.index).getPath()).into(FileBrowseActivity.this.ivContent);
                        }
                    } else if (FileBrowseActivity.this.index > 0 && FileBrowseActivity.this.url.equals("local")) {
                        FileBrowseActivity.access$410(FileBrowseActivity.this);
                        FileBrowseActivity.this.tvTitle.setText(FileBrowseActivity.this.imageWalls.get(FileBrowseActivity.this.index).getName().substring(0, FileBrowseActivity.this.imageWalls.get(FileBrowseActivity.this.index).getName().lastIndexOf(".")));
                        Glide.with((FragmentActivity) FileBrowseActivity.this).load(FileBrowseActivity.this.imageWalls.get(FileBrowseActivity.this.index).getPath()).into(FileBrowseActivity.this.ivContent);
                    }
                    if ((FileBrowseActivity.this.curPosY - FileBrowseActivity.this.posY <= 0.0f || Math.abs(FileBrowseActivity.this.curPosY - FileBrowseActivity.this.posY) <= 25.0f) && FileBrowseActivity.this.curPosY - FileBrowseActivity.this.posY < 0.0f) {
                        Math.abs(FileBrowseActivity.this.curPosY - FileBrowseActivity.this.posY);
                    }
                } else if (action == 2) {
                    FileBrowseActivity.this.curPosX = motionEvent.getX();
                    FileBrowseActivity.this.curPosY = motionEvent.getY();
                }
                return true;
            }
        });
    }

    public void showDialog(String str) {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.dialog_download_progress, (ViewGroup) null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(linearLayout).setCancelable(false);
        AlertDialog create = builder.create();
        this.dialog = create;
        create.show();
        TextView textView = (TextView) linearLayout.findViewById(R.id.tv_index);
        this.tvIndex = textView;
        textView.setText("");
        TextView textView2 = (TextView) linearLayout.findViewById(R.id.tv_file_name);
        this.tvFileName = textView2;
        textView2.setText("正在下载\"" + str + Typography.quote);
        this.tvOff = (TextView) linearLayout.findViewById(R.id.tv_off);
        this.numberProgressBar = (NumberProgressBar) linearLayout.findViewById(R.id.number_progress_bar);
        this.tvOff.setText("取消");
        this.tvOff.setOnClickListener(new View.OnClickListener() { // from class: com.gisocn.onemap.ui.activity.FileBrowseActivity.3
            @Override // android.view.View.OnClickListener
            public void onClick(View view) {
                FileBrowseActivity.this.dialog.dismiss();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void displayFile() {
        Bundle bundle = new Bundle();
        bundle.putString(TbsReaderView.KEY_FILE_PATH, getLocalFile().getPath());
        bundle.putString(TbsReaderView.KEY_TEMP_PATH, Environment.getExternalStorageDirectory().getPath());
        boolean preOpen = this.tbsReaderView.preOpen(parseFormat(this.mFileName), false);
        if (preOpen) {
            this.tbsReaderView.openFile(bundle);
        } else {
            Log.e("mFileName", this.mFileName);
            String str = this.mFileName;
            String substring = str.substring(str.lastIndexOf("."));
            if (substring.contains("png") || substring.contains("PNG") || substring.contains("jpg") || substring.contains("JPG") || substring.contains("gif") || substring.contains("GIF") || substring.contains("jpeg") || substring.contains("JPEG")) {
                Glide.with((FragmentActivity) this).load(getLocalFile().getPath()).into(this.ivContent);
            }
        }
        Log.e("preOpen_result", "" + preOpen);
    }

    private String parseFormat(String str) {
        return str.substring(str.lastIndexOf(".") + 1);
    }

    private boolean isLocalExist() {
        return getLocalFile().exists();
    }

    private File getLocalFile() {
        Log.e("getLocalFile", "" + this.fileUrl);
        if (this.url.equals("local")) {
            return new File(this.filePath, this.mFileName);
        }
        return new File(Constant.SAVE_FILE_PACKAGE + this.filePath, this.mFileName);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseActivity, androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, android.app.Activity
    public void onDestroy() {
        super.onDestroy();
        TbsReaderView tbsReaderView = this.tbsReaderView;
        if (tbsReaderView != null) {
            tbsReaderView.onStop();
        }
        this.handler.removeCallbacksAndMessages(null);
    }

    @OnClick({R.id.tv_close})
    public void onViewClicked() {
        finish();
    }

    @Override // com.gisocn.onemap.view.FileBrowseView
    public void downLoadSuccess() {
        this.handler.sendEmptyMessage(1);
    }
}
